Github,
Ledger Devs Slack
@ledgerhq/hw-transport
@ledgerhq/hw-transport
implements the generic interface of a Ledger Hardware Wallet transport.
API
Table of Contents
Subscription
Properties
unsubscribe
function (): void
Device
Type: Object
DescriptorEvent
type: add or remove event
descriptor: a parameter that can be passed to open(descriptor)
deviceModel: device info on the model (is it a nano s, nano x, ...)
device: transport specific device info
Properties
type
("add"
| "remove"
)descriptor
DescriptordeviceModel
DeviceModel??device
Device?
Observer
Type: $ReadOnly<{next: function (event: Ev): any, error: function (e: any): any, complete: function (): any}>
Transport
Transport defines the generic interface to share between node/u2f impl
A Descriptor is a parametric type that is up to be determined for the implementation.
it can be for instance an ID, an file path, a URL,...
exchange
low level api to communicate with the device
This method is for implementations to implement but should not be directly called.
Instead, the recommanded way is to use send() method
Parameters
_apdu
Bufferapdu
the data to send
Returns Promise<Buffer> a Promise of response data
setScrambleKey
set the "scramble key" for the next exchanges with the device.
Each App can have a different scramble key and they internally will set it at instanciation.
Parameters
_key
stringkey
the scramble key
close
close the exchange with the device.
Returns Promise<void> a Promise that ends when the transport is closed.
on
Listen to an event on an instance of transport.
Transport implementation can have specific events. Here is the common events:
"disconnect"
: triggered if Transport is disconnected
Parameters
off
Stop listening to an event on an instance of transport.
Parameters
setDebugMode
Enable or not logs of the binary exchange
setExchangeTimeout
Set a timeout (in milliseconds) for the exchange call. Only some transport might implement it. (e.g. U2F)
Parameters
setExchangeUnresponsiveTimeout
Define the delay before emitting "unresponsive" on an exchange that does not respond
Parameters
send
wrapper on top of exchange to simplify work of the implementation.
Parameters
cla
numberins
numberp1
numberp2
numberdata
Buffer (optional, default Buffer.alloc(0)
)statusList
Array<number> is a list of accepted status code (shorts). [0x9000] by default (optional, default [StatusCodes.OK]
)
Returns Promise<Buffer> a Promise of response buffer
isSupported
Statically check if a transport is supported on the user's platform/browser.
Type: function (): Promise<boolean>
list
List once all available descriptors. For a better granularity, checkout listen()
.
Type: function (): Promise<Array<Descriptor>>
Examples
TransportFoo.list().then(descriptors => ...)
Returns any a promise of descriptors
listen
Listen all device events for a given Transport. The method takes an Obverver of DescriptorEvent and returns a Subscription (according to Observable paradigm https://github.com/tc39/proposal-observable )
a DescriptorEvent is a { descriptor, type }
object. type can be "add"
or "remove"
and descriptor is a value you can pass to open(descriptor)
.
each listen() call will first emit all potential device already connected and then will emit events can come over times,
for instance if you plug a USB device after listen() or a bluetooth device become discoverable.
Type: function (observer: Observer<DescriptorEvent<Descriptor>>): Subscription
Parameters
observer
is an object with a next, error and complete function (compatible with observer pattern)
Examples
const sub = TransportFoo.listen({
next: e => {
if (e.type==="add") {
sub.unsubscribe();
const transport = await TransportFoo.open(e.descriptor);
...
}
},
error: error => {},
complete: () => {}
})
Returns any a Subscription object on which you can .unsubscribe()
to stop listening descriptors.
open
attempt to create a Transport instance with potentially a descriptor.
Type: function (descriptor: Descriptor, timeout: number): Promise<Transport<Descriptor>>
Parameters
descriptor
: the descriptor to open the transport with.timeout
: an optional timeout
Examples
TransportFoo.open(descriptor).then(transport => ...)
Returns any a Promise of Transport instance
create
create() allows to open the first descriptor available or
throw if there is none or if timeout is reached.
This is a light helper, alternative to using listen() and open() (that you may need for any more advanced usecase)
Parameters
openTimeout
number (optional, default 3000
)listenTimeout
number?
Examples
TransportFoo.create().then(transport => ...)
Returns Promise<Transport<Descriptor>>